﻿angular.module('app.controllers')
    .controller('SigninFormController', [
        '$scope', '$rootScope','$cookies', '$state', 'oauthService', 'accountService','APP_CONFIG',
        function ($scope,$rootScope, $cookies, $state, oauthService, accountService, APP_CONFIG) {
            accountService.logout();
            $scope.user = {};
            $scope.authError = null;
            $scope.login = function() {
                $scope.authError = '登录中。。。';
                // Try to login
                oauthService.tryLogin($scope.user.phone, $scope.user.password)
                    .then(function(result) {
                        if (result.success) {
                            accountService.getUserData()
                                .then(function(result){
                                    $rootScope.$emit(APP_CONFIG.eventUserLoaded, result.user);
                                    $state.go('app.dashboard');                                    
                                },function(x){
                                    $scope.authError='获取用户信息失败';
                                });

                        } else {
                            $scope.authError = '用户名或者密码错误';
                        }
                    }, function(x) {
                        $scope.authError = 'Server Error';
                        console.log(x);
                    });
            };

        }
    ]);